package com.byx.chatclient.utils;

import android.util.Base64;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Pattern;

/**
 * ValidateUtil
 * 参数校验的工具类
 * @author Superb
 * @date 2021/4/1 19:48
 */
public class ValidateUtil {

    private static boolean isBaseValid(String base,String pattern){
        if (base == null || base.isEmpty()) {
            return false;
        }
        return Pattern.compile(pattern).matcher(base).matches();
    }
    /**
     * 校验账号不能为空且必须是中国大陆手机号（宽松模式匹配）
      */
    public static boolean isPhoneValid(String account) {
        return isBaseValid(account,"^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$");
    }

    public static boolean isEmailValid(String email) {
        return isBaseValid(email,"^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
    }

    public static boolean isVerificationValid(String code){
        return isBaseValid(code,"^\\d{6}$");
    }

    public static boolean isPasswordValid(String password) {
        return isBaseValid(password,"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z.*]{6,20}$");
    }

    /**
     * 验证登录账号，可以是手机号，邮箱，软件账号
     * @param loginId
     * @return
     */
    public static boolean isLoginIdValid(String loginId){
        return isBaseValid(loginId, "^[1-9][0-9]\\d{3,9}$") || isPhoneValid(loginId) || isEmailValid(loginId);
    }

    public static String encodeByMd5(String str) {
        MessageDigest md5;
        try {
            md5 = MessageDigest.getInstance("md5");
            byte[] bytes = md5.digest(str.getBytes(StandardCharsets.UTF_8));
            StringBuilder result = new StringBuilder();
            for (byte b : bytes) {
                String temp = Integer.toHexString(b & 0xff);
                if (temp.length() == 1) {
                    temp = "0" + temp;
                }
                result.append(temp);
            }
            //md5加密后为32位小写,再使用Base64加密传输
            return new String(Base64.encode(result.toString().getBytes(), Base64.NO_WRAP));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String encodeByBase64(String str){
        String result = "";
        if( str != null) {
//            result = java.util.Base64.getEncoder().encodeToString(str.getBytes(StandardCharsets.UTF_8));
            result = new String(Base64.encode(str.getBytes(StandardCharsets.UTF_8), Base64.NO_WRAP), StandardCharsets.UTF_8);
        }
        return result;

    }

    public static String decodeByBase64(String encodeStr){
        String result = "";
        if (encodeStr != null) {
//            result = new String(java.util.Base64.getDecoder().decode(encodeStr),StandardCharsets.UTF_8);
            result = new String(Base64.decode(encodeStr, Base64.NO_WRAP), StandardCharsets.UTF_8);
        }
        return result;
    }
}
